<?php
/**
 * 已采购的库存设备表
 *
 * @author Zeon <scplay@gmail.com>
 * @date 2018/6/7
 * @copyright PanxSoft Inc.
 */
namespace App\Models\Panx;

use App\Models\Concerns\ScopeFilterable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

/**
 * @property Device device
 * @property int stock
 * @mixin Builder
 *
 * @method static self filterDeviceCode(?string $code)
 * @method static self filterHasStock(int $stock = 0)
 * @method static self filterDeviceUuid(?string $uuid)
 * @method static self filterDeviceName(?string $device_name)
 * @method static self filterSpecification(?string $specification)
 * @method static self filterProducer(?string $producer)
 * @method static self filterBuyer(?string $buyer)
 */
class PurchaseDevice extends Model
{
    use ScopeFilterable;

    protected $table = 'purchase_devices';

    protected $guarded = [];

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function device()
    {
        return $this->belongsTo(Device::class, 'device_id', 'id');
    }

    /**
     * @used-by filterHasStock
     * @param Builder $query
     * @param int $stock
     * @return Builder
     */
    public function scopeFilterHasStock(Builder $query, int $stock = 0)
    {
        return $query->where('stock', '>=', $stock);
    }

    /**
     * @used-by filterDeviceUuid
     * @param Builder $query
     * @param $uuid
     * @return Builder
     */
    public function scopeFilterDeviceUuid(Builder $query, $uuid)
    {
        if (! $uuid) return $query;

        return $query->whereHas('device', function(Builder $query) use($uuid){
            return $query->where('uuid','like', '%'.$uuid.'%');
        });
    }

    /**
     * @param Builder $query
     * @param null|string $code
     *
     */
    public function scopeFilterDeviceCode(Builder $query, ?string $code): void
    {
        if (is_string($code)) {
            $query->whereHas('device', function(Builder $query) use($code){
                return $query->where('code','like', '%'.$code.'%');
            });
        }
    }

    /**
     * @used-by filterDeviceName
     * @param Builder $query
     * @param $device_name
     * @return Builder
     */
    public function scopeFilterDeviceName(Builder $query, $device_name)
    {
        if (! $device_name) return $query;

        return $query->whereHas('device', function(Builder $query) use($device_name){
            return $query->where('device_name','like', '%'. $device_name.'%');
        });
    }

    /**
     * @used-by filterSpecification
     * @param Builder $query
     * @param $specification
     * @return Builder
     */
    public function scopeFilterSpecification(Builder $query, $specification)
    {
        if (! $specification) return $query;

        return $query->whereHas('device', function(Builder $query) use($specification){
            return $query->where('specification','like', '%'. $specification.'%');
        });
    }

    /**
     * @used-by filterProducer
     * @param Builder $query
     * @param $producer
     * @return Builder
     */
    public function scopeFilterProducer(Builder $query, $producer)
    {
        if (! $producer) return $query;

        return $query->whereHas('device', function(Builder $query) use($producer){
            return $query->where('producer','like', '%'. $producer.'%');
        });
    }

    /**
     * @used-by filterBuyer
     * @param Builder $query
     * @param $buyer
     * @return Builder
     */
    public function scopeFilterBuyer(Builder $query, ?string $buyer)
    {
        if (! $buyer) return $query;

        return $query->where('buyer','like', '%'. $buyer.'%');
    }
}
